# -*- coding: utf-8 -*-
import re
import urllib2
import time
import random
print(random.uniform(1, 2))

def save_to_file(file_name, contents):
    fh = open(file_name, 'w')
    fh.write(contents)
    fh.close()

print type
request = urllib2.Request('http://purepen.com/sgyy/index.htm')
response = urllib2.urlopen(request)

text = response.read()
detailUrls = re.findall('<TD><A HREF="(.*?)">', text)
for url in detailUrls[:20]:
    start_time = time.time()
    url_one = 'http://purepen.com/sgyy/{}'.format(url)
    print url_one
    request1 = urllib2.Request(url_one)
    response1 = urllib2.urlopen(request1)
    text2 = response1.read()

    read_time = time.time()
    print 'read:' + str(read_time - start_time)

    title = re.findall('<title>(.*?)</title>', text2)
    text3 = re.findall('" size="3">(.*?)</font></pre>', text2, flags = re.S)

    deil_time = time.time()
    print 'deil:' + str(deil_time - read_time)
    print 'write'
    # save_to_file('txt' + re.findall('(.*?).htm', url)[0] + '.txt', title[0] + "\n" + text3[0])
    time.sleep(random.uniform(1, 3))
